﻿using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;

namespace NDasm.Core.UI.Base
{    
    public static class ReflectionHelper
    {
        public static string GetPropertyName(LambdaExpression expression)
        {
            MemberExpression memberExpression;
            if (expression.Body is UnaryExpression)
            {
                UnaryExpression unaryExpression = expression.Body as UnaryExpression;
                memberExpression = unaryExpression.Operand as MemberExpression;
            }
            else
            {
                memberExpression = expression.Body as MemberExpression;
            }

            PropertyInfo propertyInfo = null;
            if (memberExpression != null)
            {
                propertyInfo = memberExpression.Member as PropertyInfo;
            }

            return propertyInfo != null ? propertyInfo.Name : string.Empty;     
        }

        public static bool VerifyPropertyName(object obj, string propertyName)
        {
            return TypeDescriptor.GetProperties(obj)[propertyName] != null;
        }
    }
}
